<template>
    <el-dialog
        :visible.sync="dialogVisible"
        :title="'修改规格【'+specData.specName+'】'"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
        center
    >
        <div style="max-height: 400px; overflow-y:scroll; padding-right:20px; ">
            <el-form ref="formRef" label-width="60px">
                <draggable v-model="specValueList" handle=".el-icon-rank">
                    <el-form-item v-for="(item,index) in specValueList" label-width="0" prop="list" size="mini">
                        <el-row style="width: 100%;">
                            <el-col :span="3">
                                <i class='el-icon-rank' style="cursor: move;"/> 值{{ index + 1 }}
                            </el-col>
                            <el-col :span="20">
                                <el-input v-model="item.specValue"/>
                            </el-col>
                            <el-col :span="1" style="text-align: right;">
                                <i class="el-icon-delete" @click="delItem(index)" style="cursor: pointer;" title="删除"/>
                            </el-col>
                        </el-row>
                    </el-form-item>
                </draggable>
                <el-form-item label-width="0">
                    <el-row style="width: 100%;">
                        <el-col :span="3" style="min-height:1px;"></el-col>
                        <el-col :span="20">
                            <el-button style="width:100%; font-size: 30px; padding-top:3px;padding-bottom: 7px;"
                                       @click="addItem()">
                                +
                            </el-button>
                        </el-col>
                    </el-row>
                </el-form-item>
                <!--                <el-form-item>-->
                <!--                    <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">-->
                <!--                        <el-button slot="reference" type="primary">保存</el-button>-->
                <!--                    </el-popconfirm>-->
                <!--                </el-form-item>-->
            </el-form>
        </div>

        <div slot="footer" class="dialog-footer">
            <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                <el-button slot="reference" type="primary">保存</el-button>
            </el-popconfirm>
        </div>
    </el-dialog>
</template>

<script>
import {SpecApi} from '@/api/mall/spec'
import {Message} from 'element-ui'
import draggable from 'vuedraggable'

export default {
    name: 'SpecValueUpdate',
    components: {draggable},
    data() {
        return {
            dialogVisible: false,
            specValueList: [],
            specData: {}
        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true

            SpecApi.getAllListBySpecId(data.specId).then((response) => {
                this.specValueList = response
            }).catch(() => {
            })

            this.specData = data
        },
        addItem() {
            const item = {
                specValueId: 0,
                specId: this.specData.specId,
                specValue: ''
            }

            this.specValueList.push(item)
        },

        delItem(index) {
            this.specValueList.splice(index, 1)
        },

        // 提交表单
        submitForm() {
            const that = this

            if (that.specValueList.length === 0) {
                Message({
                    message: '至少添加一个值',
                    type: 'warning'
                })

                return
            }

            SpecApi.saveSpecValue(that.specValueList).then((response) => {
                that.dialogVisible = false
                that.$emit('refresh')
            })
        }
    }
}
</script>
